package src.com.qf.dao.impl;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import src.com.qf.dao.UserDao;
import src.com.qf.entity.User;
import src.com.qf.utils.JdbcUtil;

import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    QueryRunner queryRunner = new QueryRunner();
    @Override
    public User selectUserByUsername(String username) {
        String sql = "select *from tb_user where username = ?";
        User query = null;
        try {
            query = queryRunner.query(JdbcUtil.getCon(), sql, new BeanHandler<>(User.class), username);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;

    }

    @Override
    public int insertUserByUser(User user) {
        int update = 0;
        String sql = "insert into tb_user(username,password,email,gender,role,active_code,status) values (?,?,?,?,?,?,?)";
        try {
            update = queryRunner.update(JdbcUtil.getCon(), sql, user.getUsername(), user.getPassword(), user.getEmail(), user.getGender(), user.getRole(),user.getActive_code(),user.getStatus());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return update;
    }

    @Override
    public int updateStatusByActiveCode(String activeCode) {
        int update = 0;
        String sql = "update tb_user set status=1 where active_code = ?";
        try{
            update = queryRunner.update(JdbcUtil.getCon(), sql, activeCode);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return update;
    }
}
